$(document).ready(function() {

	//~ 公告
	jQuery(".notice").slide({
		mainCell: ".bd ul",
		autoPage: true,
		effect: "topLoop",
		autoPlay: true
	});
	
	//~ 喜欢按钮提示
	$(document).on('click', '.postLike.done', function() {
		layer.tips('已喜欢', this, {
			tips: [1, '#1ABC9C']
		});
	});

	//~ 搜索框非空验证
	$(".search button").click(function() {
		if($(".search input").val() === "") {
			$(".search input").css("border-color", "red").attr('placeholder', '请输入关键字...');
			return false;
		}
	});
	$('html,body').click(function() {
		$(".search input").css("border-color", "#434649").attr('placeholder', '搜索关键字');

	});

	//~ 导航菜单
	$('.menu_li').hover(function() {
			$('.menu_ul_ul', this).slideDown(100);
		},
		function() {
			$('.menu_ul_ul', this).slideUp(100);
		}
	);

	//~ 首页幻灯片
	jQuery(".content_slide").slide({
		mainCell: ".bd ul",
		delayTime: 500,
		interTime: 5000,
		effect: "leftLoop",
		autoPlay: true
	});

	//~ 文章点赞
	$.fn.postLike = function() {
		if($(this).hasClass('done')) {
			return false;
		} else {
			$(this).addClass('done');
			var id = $(this).data("id"),
				action = $(this).data('action'),
				rateHolder = $(this).children('.count');
			var ajax_data = {
				action: "like",
				um_id: id,
				um_action: action
			};
			$.post("/wp-admin/admin-ajax.php", ajax_data,
				function(data) {
					$(rateHolder).html(data);
				});
			return false;

		}
	};

	$(".postLike").click(function() {
		var i = $(this).children('.icon-xihuant');
		i.delay(500).queue(function() {
			i.addClass('xh');
		});
	});

	$(document).on("click", ".postLike",
		function() {
			$(this).postLike();
		});
	//~ 文件下载
	$.fn.postDownload = function() {
		var id = $(this).data("id"),
			action = $(this).data('action'),
			rateHolder = $(this).children('.count');
		var ajax_data = {
			action: "download",
			um_id: id,
			um_action: action
		};
		$.post("/wp-admin/admin-ajax.php", ajax_data,
			function(data) {
				$(rateHolder).html(data);
			});
		return false;
	};
	$(document).on("click", ".postDownload",
		function() {
			$(this).postDownload();
		});

	$(function() {
		//~ 评论锚链接
		var url = window.location.toString();
		var id = url.split("#")[1];
		if(id) {
			var t = $("#" + id).offset().top;
			$(window).scrollTop(t);
		}
		$('.comment a[href*=#]').click(function() {
			if(location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
				var $target = $(this.hash);
				$target = $target.length && $target || $('[name=' + this.hash.slice(1) + ']');
				if($target.length) {
					var targetOffset = $target.offset().top - 64;
					$('html,body').animate({
						scrollTop: targetOffset
					}, 250);
					return false;
				}
			}

		});

		var comm = window.location.hash;
		if(comm === '#comments') {
			//~ 评论锚链接
			$('html,body').animate({
				scrollTop: $(comm).offset().top - 64
			}, 250);
		} else if(comm) {
			$('html,body').animate({
				scrollTop: $(comm).offset().top - 64
			}, 250);
		}

		//~ 返回顶部
		$(window).scroll(function() {
			if($(window).scrollTop() > 300) {
				$(".back_top").fadeIn(200);
			} else {
				$(".back_top").fadeOut(200);
			}	
		});
		$(".back_top").click(function() {
			$('body,html').animate({
				scrollTop: 0
			}, 200);
			return false;
		});

	});
	
	//~ 侧边栏悬浮
	$.fn.smartFloat = function() {
		var position = function(element) {
			var top = element.position().top - 65,
				pos = element.css("position");
			$(window).scroll(function() {
				var scrolls = $(this).scrollTop();
				if(scrolls > top) {
					if(window.XMLHttpRequest) {
						element.css({
							position: "fixed",
							top: 65
						});
					} else {
						element.css({
							top: scrolls
						});
					}
				} else {
					element.css({
						position: "relative",
						top: 0
					});
				}
			});
		};
		return $(this).each(function() {
			position($(this));
		});
	};
	var content = $(".content.fl").outerHeight(true);
	var sidebar = $(".sidebar.fr").outerHeight(true);
	if(content > sidebar) {
		$(".sidebar").children("div:last-child").smartFloat();
	}
});
//mousewheel
(function($){var _=["DOMMouseScroll","mousewheel"];if($.event.fixHooks)for(var B=_.length;B;)$.event.fixHooks[_[--B]]=$.event.mouseHooks;$.event.special.mousewheel={setup:function(){if(this.addEventListener){for(var $=_.length;$;)this.addEventListener(_[--$],A,false)}else this.onmousewheel=A},teardown:function(){if(this.removeEventListener){for(var $=_.length;$;)this.removeEventListener(_[--$],A,false)}else this.onmousewheel=null}};$.fn.extend({mousewheel:function($){return $?this.bind("mousewheel",$):this.trigger("mousewheel")},unmousewheel:function($){return this.unbind("mousewheel",$)}});function A(B){var _=B||window.event,A=[].slice.call(arguments,1),D=0,C=true,E=0,F=0;B=$.event.fix(_);B.type="mousewheel";if(_.wheelDelta)D=_.wheelDelta/120;if(_.detail)D=-_.detail/3;F=D;if(_.axis!==undefined&&_.axis===_.HORIZONTAL_AXIS){F=0;E=-1*D}if(_.wheelDeltaY!==undefined)F=_.wheelDeltaY/120;if(_.wheelDeltaX!==undefined)E=-1*_.wheelDeltaX/120;A.unshift(B,D,E,F);return($.event.dispatch||$.event.handle).apply(this,A)}})(jQuery);
